/**
 * 
 */
package com.hk.Mgr.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.HKNewsReq;
import com.hk.Mgr.web.message.kyc.HKNewsRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.DateUtil;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.HKNews;
import com.hk.service.kyc.INews;

/**
 * @author jeff 新闻模块
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/newsCtrl")
public class NewsCtrl extends BaseCtrl {

	@Autowired
	private INews newsService;

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<Boolean> update(@RequestBody HKNewsReq req) {
		ResponseBase<Boolean> resp = new ResponseBase<>();
		Integer id = req.getId();

		logger.info("update uews  id = " + id);
		HKNews news = newsService.findById(id);

		try {
			if (news == null) {
				resp.setMsg("参数不对：id is null！");
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				return resp;
			} else {
				ReqToModelHelper.copyReqValueToModel(req, news);
				resp.setResult(true);
				resp.setResultCode(StatusCode.SUCCESS.key());
			}
		} catch (Exception e) {
			logger.error("更新卡券失败");
			resp.setMsg("更新卡券失败，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * 获得所有活动
	 * 
	 * @author jeff
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<List<HKNewsRes>> list(@RequestBody HKNewsReq req) {

		ResponseBase<List<HKNewsRes>> resp = new ResponseBase<List<HKNewsRes>>();
		List<HKNewsRes> newsReslist = new ArrayList<HKNewsRes>();
		List<HKNews> newslist = new ArrayList<HKNews>();
		HqlFilter hqlFilter = new HqlFilter();

		logger.info("get news list type={}, channel={}", req.getType(),
				req.getChannel());

		if (StringHelper.isEmpty(req.getChannel())) {
			logger.error("news type is null");
			resp.setMsg("参数错误，请重试");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
		}

		if (req.getType() != null) {
			hqlFilter.addEQFilter("type", req.getType());
		}

		if (StringHelper.isNotEmpty(req.getChannel())) {
			hqlFilter.addLKFilter("channel", req.getChannel());
		}

		hqlFilter.addSort("cts");
		hqlFilter.addOrder("desc");

		List<HKNews> totallist = new ArrayList<HKNews>();
		totallist = newsService.findByFilter(hqlFilter);

		newslist = newsService.findByFilter(hqlFilter, req.getCurPage(),
				req.getPageSize());

		for (HKNews news : newslist) {

			HKNewsRes newsRes = new HKNewsRes();

			ReqToModelHelper.copyReqValueToModel(news, newsRes);
			if (news.getContent() != null) {
				String contentTxt;
				try {
					contentTxt = new String(news.getContent(), "UTF-8");
					newsRes.setContentStr(contentTxt);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			newsRes.setCtsStr(DateUtil.longToShortDate(news.getCts()));
			newsRes.setCtsStr(DateUtil.longToLongDate(news.getCts()));
			newsReslist.add(newsRes);
		}

		resp.setResult(newsReslist);
		resp.setSize(totallist.size());
		resp.setResultCode(StatusCode.SUCCESS.key());

		return resp;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<Boolean> save(@RequestBody HKNews req) {
		ResponseBase<Boolean> resp = new ResponseBase<>();
		try {
			newsService.add(req);
			resp.setResult(true);
			resp.setResultCode(StatusCode.SUCCESS.key());

		} catch (Exception e) {
			logger.error("add new card failed");
			resp.setMsg("保存新闻失败，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
}
